Skip to content

Conversation

@slurmlord
Copy link

This change introduces the option to generate crash dumps, aka. mini dumps, on fatal errors.
The main minidump functionality is done by explicitly loading the dbghelp.dll from the system directory, as the dbghelp.dll that is bundled with the game is an older version that does not include this functionality. There is an option to create small dumps or extended info dumps, currently both are created.

Small dumps

These mostly contain stacks for the process threads and some stack variables, or to create dumps with extended info. The use case for these is to quickly determine where a crash occured, the type of crash, if it was already fixed etc. In addition, if the memory allocation structures are corrupted enough, an extended info dump might not succeed while the small dump should. The size of these dumps are typically on the order of 250kB.

Extended info

These contain global values, along with the memory regions allocated via the memory pool factory and the dynamic memory allocator. This makes all in-game objects available to the person debugging the crash dump, so for example dt generalszh!TheWritableGlobalData in WinDbg will show the state at the time the dump was created.

An alternative option could be to not traverse the memory structures "manually" to get to the allocations and instead just specify the MiniDumpWithFullMemory flag to MiniDumpWriteDump, but that increases the file size considerably.

As an example, dump of the generalszh process in the main menu with the shell map in the background yields a ~140MB dump when traversing and ~420MB with MiniDumpWithFullMemory. Beyond that, the ~140MB file compresses to ~20MB with 7Z, so should be relatively easily transferable.

Storage Location

Crash dumps are stored in a new folder called 'CrashDumps' under the userDir ("Documents\Command and Conquer Generals Zero Hour Data"), and on startup it will create this directory if it doesn't exist and delete any older dumps so only the 10 newest small and 2 newest extended info dumps are left. This is to preserve disk space, as the extended info files can be several hundred MB.

Integration points

For VS2022 builds, unhandled exceptions end up in the UnhandledExceptionFilter in WinMain, which then get a reference to the actual exception that occurred and includes that in the dump.
For VC6 builds, unhandled exceptions are caught in the catch(...) blocks of GameEngine::execute which then calls RELEASE_CRASH. As there is no exception data available in this case to populate _EXCEPTION_POINTERS from, an intentional exception is triggered to get the trace of the current thread. This makes the stack traces for VC6 a bit more cryptic than VS2022 builds as the C++ exception handling gets included in the trace.

Limitations

In the longer run we'll probably want to replace this code with a more mature solution, like CrashPad, but that currently depends on a newer compiler than VC6.
As the code is intended to be temporary, it's kept behind a new CMake feature so it can be easily removed. There are also some other decisions made with this in mind:

  • Minidump is created in-process. Ideally, the dump should be performed by a process external to the crashing/failing process, but to avoid having to ship an extra binary, in-process was chosen instead. It's being performed in a separate thread to hopefully have a clean stack to work with.
  • Depends on RTS_BUILD_OPTION_VC6_FULL_DEBUG for VC6 builds. The PDBs generated with the default VC6 compile options are lacking in information, making the mini dumps less useful. The option RTS_BUILD_OPTION_VC6_FULL_DEBUG should be enabled for VC6 builds to ensure maximum usability. VS2022 builds produce better PDBs and require no extra options.
  • Directory management code is contained within the MiniDumper class, not re-usable by other components.
  • Many Win32-specific types and functions are used directly without regards for portability.
  • As the MiniDump feature is not available for VC6, a lot of headers have been borrowed from minidumpapiset.h and included in the MiniDumper_compat.h file.
  • Globals are used for storing the current exception info.
  • Only enabled for the games, tools are currently not included.

Copy link

@OmniBlade OmniBlade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, tested dump generation with a 2022 build.

@xezon xezon added Major Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour Debug Is mostly debug functionality System Is Systems related labels Oct 8, 2025
@slurmlord
Copy link
Author

Another approach I realized after publishing could be to move the GameMemory allocations from using GlobalAlloc to instead create a separate GameMemory heap and use HeapAlloc.
The benefit would be a lot less code required to traverse the allocations for inclusions the dump, as it could be done with HeapWalk instead for only the GameMemory heap.

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Just a bunch of small comments.

{
// Find the full path to the dbghelp.dll file in the system32 dir
GetSystemDirectory(m_sysDbgHelpPath, MAX_PATH);
strlcat(m_sysDbgHelpPath, "\\dbghelp.dll", MAX_PATH);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #1066 I added a class DbgHelpLoader that takes care of all the loading and unloading. Would be good if we can reuse that after #1066 is merged.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'll hold off on this one (but push changes for the review comments) until after #1066 is merged, then replace the loading functionality here with DbgHelpLoader and extend it with the MiniDumpWriteDump function.

m_stacktrace[0] = NULL;
}
#endif
}

This comment was marked as off-topic.

@slurmlord
Copy link
Author

Pushed a commit attempting to address most of the comments in the previous review:

  • Made TheMiniDumper a pointer, declared in MiniDumper.cpp
  • Moved extern TheMiniDumper to MiniDumper.h
  • Moved MiniDumper ctor body to MiniDumper.cpp
  • (Attempted to) find and correct all the stylistically misplaced "{"
  • Added enum for dump type
  • Fall back to DUMP_TYPE_FULL when DUMP_TYPE_GAMEMEMORY is requested and game memory implementation is turned off
  • Removed cmake feature dependency on RTS_GAMEMEMORY_ENABLE
  • Removed thread Id from dump file name
  • Replace hard-coded list of executable names to match with GetModuleFileNameW
  • (Attempted to) clean up log format
  • Added Enum values for dump thread exit codes
  • Removed MiniDumper::m_endRangeIter

In addition:

  • Added new static init/shutdown methods, using placement new on the process heap
  • Prefixed win32 api function calls with global namespace ::
  • Added asserts for dumping thread active state

Remaining:
Merge with #1066 when merged.


// Use placement new on the process heap so TheMiniDumper is placed outside the MemoryPoolFactory managed area.
// If the crash is due to corrupted MemoryPoolFactory structures, try to mitigate the chances of MiniDumper memory also being corrupted
TheMiniDumper = new (::HeapAlloc(::GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(MiniDumper))) MiniDumper;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #1066 is merged, you can also use sysAllocateDoNotZero / sysFree for it.

}

int dumpTypeFlags = MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory;
if (dumpType != DUMP_TYPE_MINIMAL)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use switch case with fall through.

int dumpTypeFlags = 0;
switch (dumpType)
{
case DUMP_TYPE_FULL:
  dumpTypeFlags |= MiniDumpWithFullMemory;
  FALLTHROUGH;
case DUMP_TYPE_GAMEMEMORY:
  dumpTypeFlags |= MiniDumpWithDataSegs | MiniDumpWithHandleData | MiniDumpWithThreadInfo | MiniDumpWithFullMemoryInfo;
  FALLTHROUGH;
case DUMP_TYPE_MINIMAL:
  dumpTypeFlags |= MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory;
  break;
}

// Only include data segments for the game and ntdll modules to keep dump size low
if (output.ModuleWriteFlags & ModuleWriteDataSeg)
{
if (::StrCmpIW(input.Module.FullPath, m_executablePath) != 0 && !::StrStrIW(input.Module.FullPath, L"ntdll.dll"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent comparison style

@xezon xezon added the Approved Pull Request was approved label Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved Pull Request was approved Debug Is mostly debug functionality Gen Relates to Generals Major Severity: Minor < Major < Critical < Blocker System Is Systems related ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants